package ProjectLogic

import (
	"CRM_rorm/logic/StaffLogic"
	"CRM_rorm/model"
	"CRM_rorm/response"
	"CRM_rorm/service"
	"github.com/gin-gonic/gin"
)

func ProjectAdd(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var project model.Project
	err := ctx.Bind(&project)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.ProjectModel.Insert(project)
	if err != nil {
		response.Fail(ctx, nil, "增加失败")
		return
	}
	response.Success(ctx, nil, "增加成功")
}

func ProjectDeleteSoft(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var project model.Project
	err := ctx.Bind(&project)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.ProjectModel.DeleteSoft(project)
	if err != nil {
		response.Fail(ctx, nil, "软删除失败")
		return
	}
	response.Success(ctx, nil, "软删除成功")
}

func ProjectDelete(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var project model.Project
	err := ctx.Bind(&project)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.ProjectModel.Delete(project)
	if err != nil {
		response.Fail(ctx, nil, "删除失败")
		return
	}
	response.Success(ctx, nil, "删除成功")
}

func ProjectFind(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var project model.Project
	err := ctx.Bind(&project)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	project, err = service.Svc.ProjectModel.FindOneByProjectId(project)
	if err != nil {
		response.Fail(ctx, nil, "查找失败")
		return
	}
	response.Success(ctx, gin.H{
		"project_id":     project.Project_id,
		"project_com_id": project.Project_Com_id,
		"project_name":   project.Project_name,
	}, "查询成功")

}

func ProjectUpdate(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var project model.Project
	err := ctx.Bind(&project)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.ProjectModel.Update(project)
	if err != nil {
		response.Fail(ctx, nil, "更新失败")
		return
	}
	response.Success(ctx, nil, "更新成功")
}
